package com.taijihuabao.modules.controller;

import com.taijihuabao.common.jsonresult.JsonResult;
import com.taijihuabao.modules.entity.UserEntity;
import com.taijihuabao.modules.model.basedto.EmailAddressDTO;
import com.taijihuabao.modules.model.user.dto.*;
import com.taijihuabao.modules.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.Duration;
import java.util.*;

/**
 * 登录注册
 *
 * @author changhui.tu
 */
@Api(tags = "登录注册")
@RestController
@RequestMapping("/api/v1")
public class LoginRegistController {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    @Autowired
    private UserService userService;

    /**
     * 获取验证码图片
     *
     * @param response
     */
    @ApiOperation("验证码图片")
    @PostMapping("/loginVailCode")
    public void getLoginVailCodeImg(HttpServletResponse response) {
        userService.getLoginVailCodeImg(response);
    }

    /**
     * 登录
     *
     * @param loginDTO
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginDTO loginDTO) {
        return JsonResult.success(userService.login(loginDTO));
    }

    /**
     * 注册
     *
     * @param registerDTO
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public JsonResult register(@RequestBody UserRegisterDTO registerDTO) {
        userService.register(registerDTO);
        return JsonResult.success();
    }

    /**
     * 注册验证码邮件
     *
     * @param emailAddressDTO
     * @return
     */
    @ApiOperation("注册验证码邮件")
    @PostMapping("/registerVailCode")
    public JsonResult emailSend(@RequestBody EmailAddressDTO emailAddressDTO) {
        userService.registerVailCode(emailAddressDTO);
        return JsonResult.success();
    }

    /**
     * 忘记密码验证码邮件
     *
     * @param emailAddressDTO
     * @return
     */
    @ApiOperation("重置密码验证码邮件")
    @PostMapping("/repeatPassVailCode")
    public JsonResult forgetLoginVailCode(@RequestBody EmailAddressDTO emailAddressDTO) {
        userService.forgetLoginVailCode(emailAddressDTO);
        return JsonResult.success();
    }

    /**
     * 重置密码验证码校验
     *
     * @param emailVailCodeDTO
     * @return
     */
    @ApiOperation("重置密码验证码校验")
    @PostMapping("/repeatPassVerify")
    public JsonResult forgetVailCodeVerify(@RequestBody UserEmailVailCodeDTO emailVailCodeDTO) {
        userService.forgetVailCodeVerify(emailVailCodeDTO);
        return JsonResult.success();
    }

    /**
     * 重置密码
     *
     * @param forgetPassDTO
     * @return
     */
    @ApiOperation("重置密码")
    @PostMapping("/repeatPass")
    public JsonResult forgetPassRepeat(@RequestBody UserForgetPassDTO forgetPassDTO) {
        userService.forgetPassRepeat(forgetPassDTO);
        return JsonResult.success();
    }

    /**
     * 用户配置redis中的键值过期时间
     *
     * @param request
     * @param t
     * @return
     */
    @ApiOperation("用户登录有效时间修改")
    @PostMapping("/updateRedisTime")
    public JsonResult updateRedisTime(HttpServletRequest request, long t) {
        String token = request.getHeader("token");
        UserEntity userEntity = (UserEntity) redisTemplate.opsForValue().get(token);
        redisTemplate.opsForValue().set(token, userEntity, Duration.ofMinutes(t));
        return JsonResult.success();
    }

    /**
     * 获取用户登录的有效时间
     *
     * @param request
     * @return
     */
    @ApiOperation("获取用户登录有效时间")
    @PostMapping("/getRedisTime")
    public JsonResult getRedisTime(HttpServletRequest request) {
        String token = request.getHeader("token");
        Long expire = redisTemplate.getExpire(token);
        return JsonResult.success(expire);
    }

    /**
     * 获取所有的存在redis中的数据
     *
     * @param request
     * @return
     */
    @ApiOperation("获取登录的用户列表")
    @PostMapping("/getRedisData")
    public JsonResult getRedisData(HttpServletRequest request) {
        String token = request.getHeader("token");
        List<Map<String, Object>> data = userService.getAllRedisVal();
        return JsonResult.success(data);
    }

}
